'use strict';

module.exports = app => {
    return class KnowledgeService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }


        /**
         * 今日作业
         * @returns {Promise<void>}
         */
        async today(params) {
            let url = this.app.urls('todayHomework');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: params
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;

        }

        /**
         * 历史作业
         * @param materialId
         * @returns {Promise<void>}
         */
        async history(params) {
            let url = this.app.urls('historyHomework');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: params
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;

        }

        /**
         * 未完成作业
         * @returns {Promise<void>}
         */
        async unfinished(params) {
            let url = this.app.urls('unfinishedTask');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: params
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;

        }



    }

};
